package com.pyb.java.commonutil.utils;

import android.util.Base64;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018/8/28 0028.
 */

public class StringUtil {

    /**
     * 数字转中文大写
     */
    private static final char[] data = new char[] { '零', '一', '二', '三', '四',
            '五', '六', '七', '八', '九' };

    private static final char[] units = new char[] { ' ', '拾', '佰', '仟', '万',
            '拾', '佰', '仟', '亿' };

    public static String convert(int money) {
        StringBuffer sbf = new StringBuffer();
        int unit = 0;
        while (money != 0) {
            sbf.insert(0, units[unit++]);
            int number = money % 10;
            sbf.insert(0, data[number]);
            money /= 10;
        }
        return sbf.toString();
    }

    /**
     * 手机号加-  133-3333-3333
     * @param phone
     * @return
     */
    public static String foramtphone(String phone){
        if (phone.length()>0){
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < phone.length(); i++) {
                builder.append(phone.charAt(i));
                if (i == 2 || i == 6 ) {
                    if (i != phone.length() - 1)
                        builder.append("-");
                }
            }
            return builder.toString();
        }else {
            return "";
        }
    }
    /**
     * 判断对象是否为空<br>
     * 1,字符串(null或者"")都返回true<br>
     * 2,数字类型(null或者0)都返回true<br>
     * 3,集合类型(null或者不包含元素都返回true)<br>
     * 4,数组类型不包含元素返回true(包含null元素返回false)<br>
     * 5,其他对象仅null返回true
     *
     * @param obj
     * @return
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) { return true; }
        if (obj instanceof Number) {
            Number num = (Number) obj;
            if (num.intValue() == 0) {
                return true;
            } else {
                return false;
            }
        } else if (obj instanceof String) {
            String str = (String) obj;
            if ((str == null) || str.equals("")) {
                return true;
            } else {
                return false;
            }
        } else if (obj instanceof Collection<?>) {
            Collection<?> c = (Collection<?>) obj;
            return c.isEmpty();
        } else if (obj instanceof Map<?, ?>) {
            Map<?, ?> m = (Map<?, ?>) obj;
            return m.isEmpty();
        } else if (obj.getClass().isArray()) {
            int length = Array.getLength(obj);
            return length == 0 ? true : false;
        } else {
            return false;
        }
    }


    /**
     * 名字条件要中文和字母判断
     *
     * @param input
     * @return
     */
    public static boolean containSpace(CharSequence input) {
        return !Pattern.compile("^[\\u4E00-\\u9FA5A-Za-z]+$").matcher(input).find()||input.length()>=14;
    }

    /**
     * base64加密
     * @param s
     * @return
     */
    public static String getBase64(String s){
        return Base64.encodeToString(s.getBytes(),Base64.DEFAULT);
    }
}
